package io.gitee.hefren.hhcache.core;

import com.google.common.base.Strings;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

/**
 * 解码器
 * @Date 2024/6/30
 * @Author lifei
 */
public class HhCacheDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> out) throws Exception {
        // 判断可读字节的长度
        if (byteBuf.readableBytes() <=0) {
            return;
        }
        //  可读长度
        int len = byteBuf.readableBytes();
        int readerIndex = byteBuf.readerIndex();
        System.out.println(Strings.lenientFormat("====> HhCacheDecoder decode len: %s, readerIndex: %s", len, readerIndex));
        byte[] bytes = new byte[len];
        byteBuf.readBytes(bytes);
        // 把字节转化成字符串
        String result = new String(bytes);
        // 把放到out
        out.add(result);
    }
}
